{
 "cells": [],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [
     "#%\n",
     "\n",
     "import pandas as pd\n",
     "from sklearn.model_selection import train_test_split, GridSearchCV\n",
     "from sklearn.preprocessing import StandardScaler\n",
     "from sklearn.neighbors import KNeighborsClassifier\n",
     "\n",
     "\n",
     "# 1. 获取数据\n",
     "facebook_data = pd.read_csv('./big_data/train.csv')\n",
     "\n",
     "facebook_data\n",
     "\n",
     "# 2. 处理数据\n",
     "# 2.1 截取一部分数据\n",
     "facebook_data = facebook_data.query('x>2 & x<5 & y>2 & y<5')\n",
     "facebook_data\n",
     "\n",
     "# 2.2 选择时间特征\n",
     "# 将Series数组（存储的是时间戳）转换为具体年月日时分秒格式\n",
     "time = pd.to_datetime(facebook_data['time'], unit='s')\n",
     "time = pd.DatetimeIndex(time)\n",
     "facebook_data['month'] = time.month\n",
     "facebook_data['day'] = time.day\n",
     "facebook_data['weekday'] = time.weekday\n",
     "facebook_data['hour'] = time.hour\n",
     "# 2.3 剔除签到少的地方的记录\n",
     "place_count = facebook_data.groupby('place_id').count().x > 3\n",
     "place_count = place_count[place_count]\n",
     "facebook_data = facebook_data[facebook_data['place_id'].isin(place_count.index)]\n",
     "# facebook_data\n",
     "\n",
     "# 2.4 提取特征值和目标值\n",
     "# 特征值\n",
     "x = facebook_data[['x', 'y', 'accuracy', 'month', 'day', 'weekday', 'hour']]\n",
     "# 目标值\n",
     "y = facebook_data['place_id']\n",
     "\n",
     "# 2.5 数据分割\n",
     "x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=2, test_size=0.2)\n",
     "# x_train\n",
     "\n",
     "\n",
     "# 3. 特征工程\n",
     "# 3.1 实例化一个转换器\n",
     "transfer = StandardScaler()\n",
     "\n",
     "# 3.2 特征初始化\n",
     "x_train = transfer.fit_transform(x_train)\n",
     "x_test = transfer.fit_transform(x_test)\n",
     "\n",
     "# 4. 机器学习（模型训练）\n",
     "# 4.1 实例化一个估计器\n",
     "estimator = KNeighborsClassifier()\n",
     "\n",
     "# 4.2 模型选择与调优，网格搜索和交叉处理\n",
     "# 超参数（k值）\n",
     "param_grid = {'n_neighbors': [3, 5, 7]}\n",
     "estimator = GridSearchCV(estimator, param_grid=param_grid, n_jobs=3,cv=10)\n",
     "\n",
     "# 4.3 机器学习\n",
     "estimator.fit(x_train, y_train)\n",
     "\n",
     "# 5. 模型评估\n",
     "# 5.1 模型测试\n",
     "y_pre = estimator.predict(x_test)\n",
     "print('预测结果和实际结果对比:\\n', y_pre == y_test)\n",
     "\n",
     "# 5.2 准确率\n",
     "score = estimator.score(x_test, y_test)\n",
     "print('准确率为:\\n', score)\n",
     "\n",
     "# 5.3 结果分析\n",
     "print(\"交叉验证中最好的结果是:\\n\", estimator.best_score_)\n",
     "print(\"最好的参数模型是:\\n\", estimator.best_estimator_)\n",
     "print(\"每次交叉验证后的准确率结果:\\n\", estimator.cv_results_)\n",
     "\n",
     "\n",
     "\n"
    ],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}